package top.yuzt.noticeserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.yuzt.noticeserver.entity.*;
import top.yuzt.noticeserver.mapper.MessagefilesMapper;
import top.yuzt.noticeserver.mapper.MessagesMapper;
import top.yuzt.noticeserver.mapper.UsersMapper;
import top.yuzt.noticeserver.service.impl.MessagesServiceImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuzt
 * @since 2024-05-05
 */
@RestController
@RequestMapping("/messages")
public class MessagesController {
    @Autowired
    private MessagesMapper messagesMapper;

    @Autowired
    private MessagefilesMapper messagefilesMapper;
    @Autowired
    private MessagesServiceImpl messagesService;

    @Autowired
    private UsersMapper usersMapper;
    @GetMapping("/getmsg")
    public List<PackMessageFileUsername> gettGroupid(Integer id){
        List<Messages> messagesList = messagesMapper.findByGroupID(id);
        List<PackMessageFileUsername> packMessageUsernameList = new ArrayList<>();
        // 对消息列表根据 sentdatetime 进行排序
        Collections.sort(messagesList, new Comparator<Messages>() {
            @Override
            public int compare(Messages m1, Messages m2) {
                // 如果 sentdatetime 是 Date 类型，则使用 compareTo 方法进行比较
                return m2.getSentdatetime().compareTo(m1.getSentdatetime());

                // 如果 sentdatetime 是 LocalDateTime 类型，则使用 compareTo 方法进行比较
                // return m1.getSentDateTime().compareTo(m2.getSentDateTime());
            }
        });
        for (Messages message : messagesList) {
            // 根据 senderid 从 users 表中获取 username
            Users user = usersMapper.findByUserID(message.getSenderid());
            List<Messagefiles> messagefilesList = messagefilesMapper.findByMessageID(message.getMessageid());
            String username = user.getUsername();
            // 将 username 设置到 message 对象中
            packMessageUsernameList.add(new PackMessageFileUsername(message,username,messagefilesList));
        }
        return packMessageUsernameList;
    }


    @GetMapping("/like")
    public List<PackMessageFileUsername> search(String keyword,Integer groupId){
        List<PackMessageFileUsername> packMessageUsernameList = new ArrayList<>();
        QueryWrapper<Messages> wrapper = new QueryWrapper<>();
        //wrapper.eq("GroupID", groupId).like("MessageText", keyword);
        wrapper.eq("GroupID", groupId).and(
                i -> i.like("Title", keyword).or().like("MessageText", keyword)
        );

        List<Messages> messagesList = messagesMapper.selectList(wrapper);

        Collections.sort(messagesList, new Comparator<Messages>() {
            @Override
            public int compare(Messages m1, Messages m2) {
                // 如果 sentdatetime 是 Date 类型，则使用 compareTo 方法进行比较
                return m2.getSentdatetime().compareTo(m1.getSentdatetime());

                // 如果 sentdatetime 是 LocalDateTime 类型，则使用 compareTo 方法进行比较
                // return m1.getSentDateTime().compareTo(m2.getSentDateTime());
            }
        });
        for (Messages message : messagesList) {
            // 根据 senderid 从 users 表中获取 username
            Users user = usersMapper.findByUserID(message.getSenderid());
            List<Messagefiles> messagefilesList = messagefilesMapper.findByMessageID(message.getMessageid());
            String username = user.getUsername();
            // 将 username 设置到 message 对象中
            packMessageUsernameList.add(new PackMessageFileUsername(message,username,messagefilesList));
        }
        return packMessageUsernameList;

    }
    @GetMapping("/delete")
     public boolean remove(Integer id){


        return messagesService.removeById(id);
     }

}
